#include "StdAfx.h"

DialogHandler* Singleton<DialogHandler>::m_instance = 0;

void DialogHandler::Render()
{
	MUTEX_LOCK(m_dlgLock);
	std::list<Dialog*>::iterator itr = m_dialogs.begin();
	std::list<Dialog*>::iterator end = m_dialogs.end();

	for( ; itr != end; ++itr)
		(*itr)->Draw();
}

void DialogHandler::SetTop(Dialog* pDialog)
{
	MUTEX_LOCK(m_dlgLock);
	std::list<Dialog*> tmp;
	std::list<Dialog*>::iterator itr = m_dialogs.begin();
	std::list<Dialog*>::iterator end = m_dialogs.end();

	for( ; itr != end; ++itr)
		if(*itr != pDialog)
			tmp.push_back(*itr);

	tmp.push_back(pDialog);
	m_dialogs = tmp;
}

bool DialogHandler::MouseActivity(bool left, Vertex2F pos, bool up)
{
	MUTEX_LOCK(m_dlgLock);
	std::list<Dialog*>::reverse_iterator itr = m_dialogs.rbegin();
	std::list<Dialog*>::reverse_iterator end = m_dialogs.rend();

	for( ; itr != end; ++itr)
	{
		if((*itr)->m_visible && (*itr)->DialogMouseActivity(left, pos, up))
		{
			SetTop(*itr);
			return true;
		}
	}

	return false;
}

void DialogHandler::AddDialog(Dialog* pDialog)
{
	MUTEX_LOCK(m_dlgLock);
	m_dialogs.push_back(pDialog);
}

void DialogHandler::OnMouseMove(ui32 x, ui32 y, bool leftDown, bool rightDown)
{
	MUTEX_LOCK(m_dlgLock);
	std::list<Dialog*>::reverse_iterator itr = m_dialogs.rbegin();
	std::list<Dialog*>::reverse_iterator end = m_dialogs.rend();

	for( ; itr != end; ++itr)
		(*itr)->OnMouseMove(x, y, leftDown, rightDown);
}